date ={
    getYear:getDate().year,
    getMonth:getDate().month,
    getDay:getDate().day,
    getDate:function(){
        let dateStr =  new Date().toISOString().slice(0, 10);
        let date = dateStr.split('-');
        return {
            date:dateStr,
            year:date[0],
            month:date[1],
            day:date[2],
        }
    },
}
db = {
    WWW_DB_PATH : "_www/static/db/wd.db",
    DOC_DB_PATH : "_doc/wd.db",
    //判断数据库是否已经准备好
    havaDB:function (){
        plus.io.resolveLocalFileSystemURL(db.DOC_DB_PATH,
            function(entry){
                console.log("成了,数据库路径是:"+entry.fullPath);
                return true;
            },
            function(e){
                console.log("错误" + JSON.stringify(e));
                return false;
            }
        )
    },
    // 打开数据库
    openDB:function (){
    	plus.sqlite.openDatabase({
    		name: 'wd',
    		path: '_doc/wd.db',
    		success: function(e){
    			console.log('openDatabase success!');
    		},
    		fail: function(e){
    			console.log('openDatabase failed: '+JSON.stringify(e));
    		}
    	});
    },
    //判断数据库是否已经打开
    isOpenDB:function (){
        return plus.sqlite.isOpenDatabase({
            name: 'wd',
            path: '_doc/wd.db',
        });
    },
    // 关闭数据库
    closeDB:function (){
    	plus.sqlite.closeDatabase({
    		name: 'first',
    		success: function(e){
    			console.log('closeDatabase success!');
    		},
    		fail: function(e){
    			console.log('closeDatabase failed: '+JSON.stringify(e));
    		}
    	});
    },
    //数据库初始化
    initDB:function (){
        plus.io.resolveLocalFileSystemURL(db.WWW_DB_PATH,
            function(entry) {
                console.log(entry.fullPath);
                //拿到源文件
                plus.io.resolveLocalFileSystemURL("_doc/",
                    function(docRoot) {
                        console.log(docRoot.fullPath);
                        //拿到目标文件夹
                        let newName = "wd.db"; //指定赋值过去之后文件名
                        entry.copyTo(docRoot, newName,
                            function(success) {
                                console.log(success);
                                return true;
                            },
                            function(e) {
                                console.log(e);
                                if (e.code == 10) {
                                    console.log("已存在db文件" + JSON.stringify(e));
                                    return 10;
                                } else {
                                    console.log("错误" + JSON.stringify(e));
                                }
                                return false;
                            }
                        )
                    }
                )
            },
            function(e) {
                console.log("错误" + JSON.stringify(e));
                return false;
            }
        )
    },
    //数据库备份
    backupDB:function (){
        console.log("backup");
        plus.io.resolveLocalFileSystemURL(db.DOC_DB_PATH,
            function(entry) {
                console.log(entry.fullPath);
                //拿到源文件
                plus.io.resolveLocalFileSystemURL("_documents/",
                    function(docRoot) {
                        console.log(docRoot.fullPath);
                        //拿到目标文件夹
                        let date = new Date();
                        let newName = date + "wd.db"; //指定赋值过去之后文件名
                        entry.copyTo(docRoot, newName,
                            function(success) {
                                console.log(success);
                            },
                            function(e) {
                                console.log(e);
                                if (e.code == 10) {
                                    console.log("已存在db文件" + JSON.stringify(e));
                                } else {
                                    console.log("错误" + JSON.stringify(e));
                                }
                            }
                        )
                    }
                )
            },
            function(e) {
                console.log("错误" + JSON.stringify(e));
            }
        )
    },
    //执行SQL
    executeSql:function (sql,successCB,failCB){
        plus.sqlite.executeSql({
            name:'wd',
            sql:sql,
            success:successCB,
            fail:failCB
        })
    },
    //查询SQL
    selectSQL:function (sql,successCB,failCB) {
        plus.sqlite.selectSql({
            name: 'wd',
            sql: sql,
            success: successCB,
            fail: failCB
        });
    },
    //查询当月收支
    currentMonthI:function(successCB,failCB){
        let year = date.getDate().year;
        let month = date.getDate().month;
        let sql = "select sum(maney) sum,substr(type,1,1) io,substr(date,1,4) year,substr(date,6,2) month from main_table where year = '"+year+"' and month = '"+month+"' and io = 'i' GROUP by year,month order by io"
        console.log(sql);
        plus.sqlite.selectSql({
            name: 'wd',
            sql: sql,
            success: successCB,
            fail: failCB
        });
    },
    //查询当月收支
    currentMonthO:function(successCB,failCB){
        let year = date.getDate().year;
        let month = date.getDate().month;
        let sql = "select sum(maney) sum,substr(type,1,1) io,substr(date,1,4) year,substr(date,6,2) month from main_table where year = '"+year+"' and month = '"+month+"' and io = 'o' GROUP by year,month order by io"
        console.log(sql);
        plus.sqlite.selectSql({
            name: 'wd',
            sql: sql,
            success: successCB,
            fail: failCB
        });
    },
}

module.exports = {
    db:db,
    date:date,
}